package hiring.tong.com.talent.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import hiring.tong.com.common.Constant;
import hiring.tong.com.common.PageModel;
import hiring.tong.com.talent.dao.ResumeDao;
import hiring.tong.com.talent.entity.ResumeEntity;
import hiring.tong.com.talent.entity.TalentEntity;

/**
 * Servlet implementation class ResumeServlet
 */
public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ResumeDao resumeDao=new ResumeDao();   

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String command=request.getParameter("command");
		if("list".equals(command)){
			list(request,response);
		}
		else if("add".equals(command)){
			add(request,response);
		}
		else if("preupdate".equals(command)){
			preupdate(request,response);
		}
		else if("delete".equals(command)){
			delete(request,response);
		}
		else if("get".equals(command)){
			get(request,response);
		}
		else if("update".equals(command)){
			update(request,response);
		}
		else{
			throw new RuntimeException("非法操作");
		}
		
		
	}
	//修改简历
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
    	int id=Integer.parseInt(request.getParameter("id"));
    	String intention=request.getParameter("intention");
		String workExperience=request.getParameter("workExperience");
		String projectExperience=request.getParameter("projectExperience");
		ResumeEntity resume=new ResumeEntity();
		resume.setId(id);
		resume.setIntention(intention);
		resume.setWorkExperience(workExperience);
		resume.setProjectExperience(projectExperience);
		int row =resumeDao.update(resume);
		request.getRequestDispatcher("ResumeServlet?command=preupdate&rows="+row).forward(request, response);
		
    	
		
	}

	//查询简历
	private void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
		ResumeEntity resume=resumeDao.get(id);
		request.setAttribute("resume", resume);
		request.getRequestDispatcher("view/talent/resume-update.jsp").forward(request, response);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
		int row =resumeDao.delete(id);
		request.getRequestDispatcher("ResumeServlet?command=preupdate&row="+row).forward(request, response);
		
	}

	private void preupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int row =0;
		String rowStr=request.getParameter("row");
		String rowStrs=request.getParameter("rows");
		if(rowStr!=null){
			row=Integer.parseInt(rowStr);
			if(row==1){
				request.setAttribute("mession", "删除成功");	
			}
			else{
				request.setAttribute("mession", "删除失败");	
			}
		}
		if(rowStrs!=null){
			row=Integer.parseInt(rowStrs);
			if(row==1){
				request.setAttribute("mession", "修改成功");	
			}
			else{
				request.setAttribute("mession", "修改失败");	
			}
		}
		TalentEntity talent=(TalentEntity)request.getSession().getAttribute("talent");
		int talent_id=talent.getId();
		
		int pageNo=Constant.DEFAULT_PAGE_NO;
		String pageStr=request.getParameter("pageNo");
		if(pageStr!=null){
			pageNo=Integer.parseInt(pageStr);
		}
		ResumeEntity resume=new ResumeEntity();
		resume.setTalentId(talent_id);
		PageModel<ResumeEntity> pagemodel=resumeDao.list(resume,pageNo,Constant.DEFAULT_PAGE_SIZE);
		request.setAttribute("pagemodel", pagemodel);
		request.getRequestDispatcher("view/talent/resume-delete.jsp").forward(request, response);
		
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		TalentEntity talent=(TalentEntity)request.getSession().getAttribute("talent");
		int talent_id=talent.getId();
		String intention=request.getParameter("intention");
		String workExperience=request.getParameter("workExperience");
		String projectExperience=request.getParameter("projectExperience");
		ResumeEntity resume=new ResumeEntity();
		resume.setTalentId(talent_id);
		resume.setIntention(intention);
		resume.setProjectExperience(projectExperience);
		resume.setWorkExperience(workExperience);
		int row=resumeDao.add(resume);
		if(row==1){
			request.setAttribute("row", row);
			request.getRequestDispatcher("view/talent/resume-add.jsp").forward(request, response);
		}
		else{
			request.setAttribute("mession", "发布失败");
			request.getRequestDispatcher("view/talent/resume-add.jsp").forward(request, response);
		}
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		TalentEntity talent=(TalentEntity)request.getSession().getAttribute("talent");
		int talent_id=talent.getId();
		System.out.println(talent_id);
		int pageNo=Constant.DEFAULT_PAGE_NO;
		String pageStr=request.getParameter("pageNo");
		if(pageStr!=null){
			pageNo=Integer.parseInt(pageStr);
		}
		ResumeEntity resume=new ResumeEntity();
		resume.setTalentId(talent_id);
		PageModel<ResumeEntity> pagemodel=resumeDao.list(resume,pageNo,Constant.DEFAULT_PAGE_SIZE);
		List<ResumeEntity> resumeList=resumeDao.list(talent_id);
		request.setAttribute("pagemodel", pagemodel);
		request.setAttribute("resumeList", resumeList);
		request.getRequestDispatcher("view/talent/resume-query.jsp").forward(request,response);
	}

	
	
	
	
	
	
}
